package com.bwhx.eruler.service.hodo.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.casdilly.BodyItem;
import com.bwhx.eruler.bean.casdilly.ClothesType;
import com.bwhx.eruler.bean.hodo.ClothesSize;
import com.bwhx.eruler.dao.casdilly.ClothesTypeDAO;
import com.bwhx.eruler.dao.hodo.ClothesSizeDAO;
 import com.bwhx.eruler.service.hodo.ClothesSizeService;
import com.bwhx.eruler.service.hodo.SizeRuleSGirthService;

@Service
public class ClothesSizeServiceImpl implements ClothesSizeService {
	
	@Autowired
	ClothesSizeDAO clothesSizeDAO;
	
	@Autowired
	ClothesTypeDAO clothesTypeDAO;
	



	@Override
	public List<ClothesSize> list(Page p) {
		return clothesSizeDAO.list(p);
	}



	@Override
	public Integer count(ClothesSize SizeRule) {
		return clothesSizeDAO.count(SizeRule);
	}



	@Override
	public void insert(ClothesSize SizeRule) {
		clothesSizeDAO.insert(SizeRule);
	}



	@Override
	public void update(ClothesSize sizeRule) {
		clothesSizeDAO.update(sizeRule);
		
	}



	@Override
	public void delete(ClothesSize sizeRuleXK) {
		clothesSizeDAO.delete(sizeRuleXK);
		
	}



	@Override
	public List<ClothesSize> findConflict(ClothesSize sizeRuleXK) {
		 return clothesSizeDAO.findConflict(sizeRuleXK);
	}



	@Override
	public List<HashMap<String, String>> detailList(Page p) {
		return clothesSizeDAO.detailList(p);
	}



	@Override
	public Integer detailCount(ClothesSize cSize) {
		return clothesSizeDAO.detailCount(cSize);
	}



	@Override
	@Transactional
	public List<Map<String,String>> findByClothesSizeID(String csid) {
		
		ClothesSize cs = clothesSizeDAO.findByClothesSizeID(csid);
		
		ClothesType clothesType = new ClothesType();
		clothesType.setTypeID(cs.getClothesTypeID());
		ClothesType resultClothesType = clothesTypeDAO.findByID(clothesType);
		
		String[] names = resultClothesType.getTypeItemsName().split(",");
		String[] ids = resultClothesType.getTypeItemsStr().split(",");
		
		
		
		List<Map<String,String>> resultCSD = clothesSizeDAO.findCSDetailBycsID(csid);
		
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		
		for (Map<String, String> map : resultCSD) {
			
			HashMap<String, String> resultMap = new HashMap<String, String>();
			
 			resultMap.put("sizeid", String.valueOf(map.get("sizeid")));
			resultMap.put("csName", map.get("csName") );
			resultMap.put("heightkey", map.get("heightkey") );
			resultMap.put("widthkey", map.get("widthkey") );
			resultMap.put("sizekey", map.get("sizekey") );
			//resultMap.put("gender",     String.valueOf(map.get("gender")).equals(true)?"1":"0" );
			resultMap.put("gender",     String.valueOf(map.get("gender")));
			 
			
			for (int i = 0; i < ids.length; i++) {		
 				String resultBodyItemValue =  map.get("bodyitem_"+ids[i]) ;
				resultMap.put(names[i], resultBodyItemValue);
			}
			
			resultList.add(resultMap);
			
			

 		}
 
 		return resultList;
	}



	@Transactional
	@Override
	public Map<String, Object> findAllClothesSize() {
		
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		 
		
		List<ClothesSize> list = clothesSizeDAO.listAll();
		for (ClothesSize clothesSize : list) {
			List<Map<String,String>> csdResultList = findByClothesSizeID(clothesSize.getCsID());
			resultMap.put(clothesSize.getCsID(), csdResultList);
		}
		
		
		
		
 		return resultMap;
	}



	@Override
	public List<ClothesSize> listAll() {
 		return   clothesSizeDAO.listAll();
	}



	@Override
	public List<Map<String, Object>> findDetailConflict(Map<String, Object> paramMap) {
 		return clothesSizeDAO.findDetailConflict( paramMap);
	}



	@Override
	public void insertDetail(Map<String, Object> paramMap) {
		clothesSizeDAO.insertDetail(paramMap);
	}



	@Override
	public void updateDetail(Map<String, Object> paramMap) {
		clothesSizeDAO.updateDetail(paramMap);
	}



	@Override
	public void delDetail(HashMap<String, Object> paramMap) {
		clothesSizeDAO.delDetail(paramMap);
	}
 
	
}
